package com.hxd.mall.utils.open

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import java.util.HashMap

/**
 * json与bean转换
 */
object GsonUtil {
    fun <T> json2bean(json: String, clazz: Class<T>): T {
        val gson = Gson()
        return gson.fromJson(json, clazz)
    }

    fun bean2json(`object`: Any): String {
        val gson = Gson()
        return gson.toJson(`object`)
    }

    fun parseMap(data: String): HashMap<String, String>? {
        val gb = GsonBuilder()
        val g = gb.create()
        return g.fromJson<HashMap<String, String>>(data, object : TypeToken<HashMap<String, String>>() {
        }.type)
    }

    fun <T> parseList(data: String): MutableList<T>? {
        val gb = GsonBuilder()
        val g = gb.create()
        return g.fromJson<MutableList<T>>(data, object : TypeToken<MutableList<T>>() {
        }.type)
    }


}